/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "camera_panel.h"

#include "vector_widget.h"

#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QVBoxLayout>
#include <QHBoxLayout>

namespace Ui {

Camera_Panel::Camera_Panel(QWidget* _parent = 0,Sirat::Common::Camera* _camera = 0) : QWidget(_parent) , camera(_camera) {
	
	QVBoxLayout* vboxlayout = new QVBoxLayout();
	
	QHBoxLayout* hboxlayout1 = new QHBoxLayout();
	width_spinbox = new QSpinBox(this);
	width_spinbox->setMaximum(9999);
	QLabel* width_spinbox_label = new QLabel(tr("Width"));
	
	hboxlayout1->addWidget(width_spinbox_label);
	hboxlayout1->addStretch();
	hboxlayout1->addWidget(width_spinbox);
		
	QHBoxLayout* hboxlayout2 = new QHBoxLayout();
	height_spinbox = new QSpinBox(this);
	height_spinbox->setMaximum(9999);
	QLabel* height_spinbox_label = new QLabel(tr("Height"));
	
	hboxlayout2->addWidget(height_spinbox_label);
	hboxlayout2->addStretch();
	hboxlayout2->addWidget(height_spinbox);
	
	vboxlayout->addLayout(hboxlayout1);
	vboxlayout->addLayout(hboxlayout2);
	
	observer_position = new Vector_Widget(this);
	QLabel* observer_position_label = new QLabel(tr("Observer's Position"));
		
	observer_up = new Vector_Widget(this);
	QLabel* observer_up_label = new QLabel(tr("Observer's Up"));
	
	grid_position = new Vector_Widget(this);
	QLabel* grid_position_label = new QLabel(tr("Grid's Position"));
	
	vboxlayout->addWidget(observer_position_label);
	vboxlayout->addWidget(observer_position);
	vboxlayout->addWidget(observer_up_label);
	vboxlayout->addWidget(observer_up);
	vboxlayout->addWidget(grid_position_label);
	vboxlayout->addWidget(grid_position);
	vboxlayout->addStretch();
	
	setLayout(vboxlayout);
	
	create_connections();
}

Camera_Panel::~Camera_Panel(void) { }
		
Sirat::Common::Camera* Camera_Panel::get_camera(void) const {
	return camera;
}

void Camera_Panel::set_camera(Sirat::Common::Camera* _camera) {
	if(_camera != 0) {
		disconnect(observer_position,SIGNAL(vector_updated()),this,SLOT(update_camera(void)));
		disconnect(grid_position,SIGNAL(vector_updated()),this,SLOT(update_camera(void)));
		disconnect(observer_up,SIGNAL(vector_updated()),this,SLOT(update_camera(void)));
		disconnect(height_spinbox,SIGNAL(valueChanged(int)),this,SLOT(update_height(int)));
		disconnect(width_spinbox,SIGNAL(valueChanged(int)),this,SLOT(update_width(int)));
		
		camera = _camera;
		grid = camera->get_grid();
		observer = camera->get_observer();
		
		height_spinbox->setValue(grid->get_height());
		width_spinbox->setValue(grid->get_width());
		
		observer_position->set_vector(observer->get_position());
		observer_up->set_vector(observer->get_up());
		grid_position->set_vector(grid->get_position());
		
		create_connections();
	}
}

void Camera_Panel::update_camera(void) {
	if(camera != 0) {
		camera->look_at(observer_position->get_vector(),grid_position->get_vector(),observer_up->get_vector());
		grid = camera->get_grid();
		observer = camera->get_observer();
	}
}

void Camera_Panel::update_height(int _height) {
	if(grid != 0) 
		grid->set_height(_height);	
}
void Camera_Panel::update_width(int _width) {
	if(grid != 0) 
		grid->set_width(_width);	
}

void Camera_Panel::create_connections(void) {
	connect(observer_position,SIGNAL(vector_updated()),this,SLOT(update_camera(void)));
	connect(grid_position,SIGNAL(vector_updated()),this,SLOT(update_camera(void)));
	connect(observer_up,SIGNAL(vector_updated()),this,SLOT(update_camera(void)));
	connect(height_spinbox,SIGNAL(valueChanged(int)),this,SLOT(update_height(int)));
	connect(width_spinbox,SIGNAL(valueChanged(int)),this,SLOT(update_width(int)));
}

}
